<?php
/*This file works with the registration form, validating data and then inserting it into
the database.*/

class Register extends Controller {
	
	function Register()
	{
		parent::Controller();
	}
	
	function index()
	{
		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->database();
		
		$data['title'] = "Register - Farm101";
		$data['NoName'] = "You may only register if you have a Registration Code.";
		$data['heading'] = "<h2 class='centertext'>Register</h2>";
		$data['formErrorWidth'] = "style='width:205px'";
		$data['wrapperErrorWidth'] = "style='width:500px'";
		

		
		$formRules = array(
			array(
				'field' => 'username',
				'label' => 'Username',
				'rules' => 'trim|required|min_length[5]|max_length[15]|alpha_numeric|xss_clean|callback_userCheck'
				),
			array(
				'field' => 'password',
				'label' => 'Password',
				'rules' => 'trim|required|matches[password2]|xss_clean|'
				),
			array(
				'field' => 'password2',
				'label' => 'Password Confirmation',
				'rules' => 'trim|required|xss_clean|'
				),
			array(
				'field' => 'email',
				'label' => 'Email',
				'rules' => 'trim|required|valid_email|xss_clean'
				),
			array(
				'field' => 'regcode',
				'label' => 'Registration Code',
				'rules' => 'trim|required|xss_clean|callback_RegistrationCheck'
				)
			);
			
		$this->form_validation->set_rules($formRules);


		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('openheader', $data);
			$this->load->view('register', $data);
		}
		else
		{
			$formUser = $_POST['username'];
			$formPass = md5($_POST['password']);
			$formEmail = ($_POST['email']);
			$formRegCode =  $_POST['regcode'];
			
			$sql = "INSERT INTO User VALUES (".$this->db->escape($formUser).", ".$this->db->escape($formPass).", ".$this->db->escape($formEmail).", ".$this->db->escape($formRegCode).")";
			$this->db->query($sql);
			
			$data['title'] = "Success! - Farm101";
			$data['heading'] = '<h1 class="centertext">Congratulations!</h1>';
			$data['text'] = "<p>You have successfully registered with Farm101, congratulations!  This means that you may now login and access the internal part of the website.  Please <a href='/index.php/open/index/'>login here</a>.";
			$data[''] = '';
			
			$this->load->view('openheader', $data);
			$this->load->view('formsuccess', $data);
			
			//echo $formUser,"<br />", $formPass, "<br />", $formEmail, "<br />", $formRegCode;
		}
	}
	
	function RegistrationCheck($code)
	{
		if ($code != 'lemons')
		{
			$this->form_validation->set_message('RegistrationCheck', 'Error:  Wrong Registration Code.');
			return FALSE;

		}
		else
		{
			return TRUE;			
		}	
	}
	
	function userCheck($name)
	{
		$this->load->database();
		
		$query = $this->db->query("SELECT Username FROM User WHERE Username='$name'");
		
		if ($query->num_rows() > 0)
		{
			

			$this->form_validation->set_message('userCheck', 'Error:  This name is already taken.');
			return FALSE;	
		}
		else
		{
			$this->form_validation->set_message('userCheck', 'This % is okay.');	//This doesn't work.... oh well
			return TRUE;
			
		}		
	}
	
}


?>